/*
 * config.cpp
 *
 *  Created on: 14.02.2009
 *      Author: george
 */

#include "config.h"

MyConfig::MyConfig(const QString & f, const int h, const int w){
	setFixedSize(w,h);
	filename = f;
	QFile data(filename);
	if (data.exists())
	if (data.open(QFile::ReadOnly)){
		QTextStream fin(&data);
		QString line;
		do{
			line = fin.readLine();
			if (line.isNull()) break;
			QStringList s = line.split(":");
			conf[s[0]] = s[1].toInt();
		}while(!line.isNull());
		data.close();
	}

	QLabel * addByLabel = new QLabel("Add by:");
	QLabel * addLabel = new QLabel("Add:");
	QLabel * saveLabel = new QLabel("Save:");
	QLabel * resetLabel = new QLabel("Reset:");
	QLabel * nextItemLabel = new QLabel("Next item:");
	QLabel * prevItemLabel = new QLabel("Previous item:");
	QLabel * editItemLabel = new QLabel("Edit Item:");
	QLabel * delItemLabel = new QLabel("Delete Item:");
	QLabel * newItemLabel = new QLabel("New Item:");
	QLabel * startLabel = new QLabel("Run line:");
	QLabel * stopLabel = new QLabel("Stop line:");

	defConf["addby"] = 1;
	defConf["add"] = 0x35;
	defConf["save"] = 0x32;
	defConf["reset"] = 0x30;
	defConf["next"] = 0x01000015;
	defConf["prev"] = 0x01000013;
	defConf["edit"] = 0x01010000;
	defConf["del"] = 0x01000003;
	defConf["new"] = 0x01000001;
	defConf["start"] = 0x33;
	defConf["stop"] = 0x31;
	
//	conf = defConf;
	
	keyMap["Select"] = 0x01010000;
	keyMap["Left"] = 0x01000012;
	keyMap["Up"] = 0x01000013;
	keyMap["Right"] = 0x01000014;
	keyMap["Down"] = 0x01000015;
	keyMap["Clear"] = 0x01000003;
	keyMap["Left tab"] = 0x01000001;
	keyMap["Back tab"] = 0x01000061;
	keyMap["0"] = 0x30;
	keyMap["1"] = 0x31;
	keyMap["2"] = 0x32;
	keyMap["3"] = 0x33;
	keyMap["4"] = 0x34;
	keyMap["5"] = 0x35;
	keyMap["6"] = 0x36;
	keyMap["7"] = 0x37;
	keyMap["8"] = 0x38;
	keyMap["9"] = 0x39;
	keyMap["*"] = 42;
	keyMap["#"] = 35;
	
	keys = QStringList(keyMap.keys());
	keys.sort();
	
	addByCombo = new QSpinBox();
	addByCombo->setRange(1,20);
	addCombo = new QComboBox();
	saveCombo = new QComboBox();
	resetCombo = new QComboBox();
	nextItemCombo = new QComboBox();
	prevItemCombo = new QComboBox();
	editItemCombo = new QComboBox();
	delItemCombo = new QComboBox();
	newItemCombo = new QComboBox();
	startCombo = new QComboBox();
	stopCombo = new QComboBox();

	addCombo->addItems(keys);
	saveCombo->addItems(keys);
	resetCombo->addItems(keys);
	nextItemCombo->addItems(keys);
	prevItemCombo->addItems(keys);
	editItemCombo->addItems(keys);
	delItemCombo->addItems(keys);
	newItemCombo->addItems(keys);
	startCombo->addItems(keys);
	stopCombo->addItems(keys);
	
	fillInTheBlank();

	QGridLayout * grid = new QGridLayout;
	grid->setSpacing(0);
	grid->addWidget(addByLabel,0,0);
	grid->addWidget(addLabel,1,0);
	grid->addWidget(saveLabel,2,0);
	grid->addWidget(resetLabel,3,0);
	grid->addWidget(nextItemLabel,4,0);
	grid->addWidget(prevItemLabel,5,0);
	grid->addWidget(editItemLabel,6,0);
	grid->addWidget(delItemLabel,7,0);
	grid->addWidget(newItemLabel,8,0);
	grid->addWidget(startLabel,9,0);
	grid->addWidget(stopLabel,10,0);

	grid->addWidget(addByCombo,0,1);
	grid->addWidget(addCombo,1,1);
	grid->addWidget(saveCombo,2,1);
	grid->addWidget(resetCombo,3,1);
	grid->addWidget(nextItemCombo,4,1);
	grid->addWidget(prevItemCombo,5,1);
	grid->addWidget(editItemCombo,6,1);
	grid->addWidget(delItemCombo,7,1);
	grid->addWidget(newItemCombo,8,1);
	grid->addWidget(startCombo,9,1);
	grid->addWidget(stopCombo,10,1);

	QVBoxLayout * main = new QVBoxLayout;
	main->setSpacing(1);

	QHBoxLayout * btns = new QHBoxLayout;
	save = new QPushButton("Save");
	defaults = new QPushButton("Default");
	cancel = new QPushButton("Cancel");
	btns->addWidget(save);
	btns->addWidget(defaults);
	btns->addWidget(cancel);

	main->addLayout(grid);
	main->addLayout(btns);
	main->addStretch();
	setLayout(main);

	QObject::connect(addByCombo,SIGNAL(valueChanged(int)),this,SLOT(addByCnf(const int)));
	QObject::connect(addCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(addCnf(const QString &)));
	QObject::connect(saveCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(saveCnf(const QString &)));
	QObject::connect(resetCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(resetCnf(const QString &)));
	QObject::connect(nextItemCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(nextCnf(const QString &)));
	QObject::connect(prevItemCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(prevCnf(const QString &)));
	QObject::connect(editItemCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(editCnf(const QString &)));
	QObject::connect(delItemCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(delCnf(const QString &)));
	QObject::connect(newItemCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(newCnf(const QString &)));
	QObject::connect(startCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(startCnf(const QString &)));
	QObject::connect(stopCombo,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(stopCnf(const QString &)));

	QObject::connect(cancel,SIGNAL(clicked()),this,SLOT(close()));
	QObject::connect(defaults,SIGNAL(clicked()),this,SLOT(toDefaults()));
	QObject::connect(defaults,SIGNAL(clicked()),this,SLOT(fillInTheBlank()));
	QObject::connect(save,SIGNAL(clicked()),this,SLOT(saveAndClose()));
	update();
}

void MyConfig::saveAndClose(){
	QFile data(filename);
	if (data.open(QFile::WriteOnly)){
		QTextStream fout(&data);
		QHashIterator<QString, int> i(conf);
		while (i.hasNext()) {
			i.next();
			fout << i.key() << ":" << i.value() << endl;
		}
		fout.flush();
		data.close();
	}
	hide();
}
void MyConfig::toDefaults(){conf = defConf;}
void MyConfig::fillInTheBlank(){
addByCombo->setValue(conf["addby"]);
addCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["add"])));
saveCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["save"])));
resetCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["reset"])));
nextItemCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["next"])));
prevItemCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["prev"])));
editItemCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["edit"])));
delItemCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["del"])));
newItemCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["new"])));
startCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["start"])));
stopCombo->setCurrentIndex(keys.indexOf(keyMap.key(conf["stop"])));
}
void MyConfig::addByCnf(const int k){conf["addby"] = k;}
void MyConfig::addCnf(const QString & s){conf["add"] = keyMap[s];}
void MyConfig::saveCnf(const QString & s){conf["save"] = keyMap[s];}
void MyConfig::resetCnf(const QString & s){conf["reset"] = keyMap[s];}
void MyConfig::nextCnf(const QString & s){conf["next"] = keyMap[s];}
void MyConfig::prevCnf(const QString & s){conf["prev"] = keyMap[s];}
void MyConfig::editCnf(const QString & s){conf["edit"] = keyMap[s];}
void MyConfig::delCnf(const QString & s){conf["del"] = keyMap[s];}
void MyConfig::newCnf(const QString & s){conf["new"] = keyMap[s];}
void MyConfig::startCnf(const QString & s){conf["start"] = keyMap[s];}
void MyConfig::stopCnf(const QString & s){conf["stop"] = keyMap[s];}
